/**
 * Created by ACER on 2017/4/16.
 */
app.controller('others-history-release', function ($scope, $http) {

    $scope.userId = null;
    $scope.taskList = [];
    $scope.page = 1;

    $scope.getUserInfo = function () {
        var userId = sessionStorage.getItem('otheruserid');
        if (userId) {
            $scope.userId = userId;
        } else {
            $.alert('未知的错误', function () {
                history.back();
            });
        }
    };

    $scope.initPage = function () {
        $scope.getUserInfo();
        $scope.getTaskList();
    };

    // 获取任务列表
    $scope.getTaskList = function (page) {

        if (!$scope.userId) {
            return;
        }

        // 准备工作
        if (!page) {
            $scope.taskList = [];
        }

        page = page ? page : 1;
        $.showPreloader('获取中...');
        $http({
            method: 'get',
            url: app.netPath + '/tasks/showOtherUserHistoryTask',
            params: {page: page, userId: $scope.userId}
        }).then(
            function (res) {

                if (!res.data.error) {
                    var taskList = res.data.data;
                    for (var i = 0; i < taskList.length; i++) {
                        $scope.taskList.push(taskList[i]);
                    }
                    var infiniteScroll = $('.infinite-scroll');
                    var infiniteScrollPreloader = $('.infinite-scroll-preloader');
                    // 启用无限加载事件
                    $.attachInfiniteScroll(infiniteScroll);
                    // 启用提示符
                    infiniteScrollPreloader.show();

                    if (res.data.allPage <= $scope.page) {
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        $.detachInfiniteScroll(infiniteScroll);
                        // 删除加载提示符
                        infiniteScrollPreloader.hide();
                    }
                } else {
                    $.alert(res.data.msg);
                }

                $.hidePreloader();
            },
            function () {
                $.hidePreloader();
                $.alert('网络走丢了~');
            }
        )
    };

    $scope.turnDetail = function(task){
        sessionStorage.setItem('taskhomeid',task.taskId);
        location.href = '#others-task-details';
    }


});